# 1、通过python连接pymysql数据库，封装类方法

# 1.1、随机插入100条数据到数据库表，包括姓名、年龄、家庭住址、身份证号码

# 1.2、查询全部的数据

# 1.3、删除指定一条数据

# 1.4、封装批量删除方法，可以删除多条

# 1.5、可以指定增加单独一条数据到表里

# 以上每个操作步骤要写日志信息到log.txt里
# import pymysql
# import random
# import time
# import logging
#
# # 配置日志
# logging.basicConfig(filename='log.txt', level=logging.INFO,
#                     format='%(asctime)s - %(levelname)s - %(message)s')
#
#
# class DatabaseManager:
#     def __init__(self, host, user, password, db):
#         self.host = host
#         self.user = user
#         self.password = password
#         self.db = db
#         self.connection = None
#
#     def connect(self):
#         try:
#             self.connection = pymysql.connect(host=self.host,
#                                               user=self.user,
#                                               password=self.password,
#                                               db=self.db)
#             logging.info("Connected to the database successfully!")
#         except Exception as e:
#             logging.error("Failed to connect to the database: {}".format(e))
#             return False
#         return True
#
#     def close(self):
#         if self.connection:
#             self.connection.close()
#             logging.info("Disconnected from the database!")
#
#     def insert_data(self):
#         try:
#             with self.connection.cursor() as cursor:
#                 for _ in range(100):
#                     name = f"Name{random.randint(1, 100)}"
#                     age = random.randint(1, 100)
#                     address = f"Address{random.randint(1, 100)}"
#                     id_card = f"ID{random.randint(1, 100)}"  # 示例身份证号格式，实际使用时请注意格式和规则。
#                     sql = "INSERT INTO `your_table_name` (`name`, `age`, `address`, `id_card`) VALUES (%s, %s, %s, %s)"  # 请替换为你的表名和列名。
#                     cursor.execute(sql, (name, age, address, id_card))
#             self.connection.commit()
#             logging.info("Inserted 100 rows of data successfully!")
#         except Exception as e:
#             logging.error("Failed to insert data: {}".format(e))
#             return False
#         return True
#
#     def select_all_data(self):
#         try:
#             with self.connection.cursor() as cursor:
#                 sql = "SELECT * FROM `your_table_name`"  # 请替换为你的表名。
#                 cursor.execute(sql)
#                 result = cursor.fetchall()  # 获取所有数据。
#             return result
#         except Exception as e:
#             logging.error("Failed to select data: {}".format(e))
#             return None
#
#     def delete_data(self, condition):  # 假设condition是一个字典，包含删除条件。例如：{'name': 'Name1', 'age': 20}。
#         try:
#             with self.connection.cursor() as cursor:
#                 sql = "DELETE FROM `your_table_name` WHERE %s"  # 请替换为你的表名。根据条件构建SQL语句。
#                 cursor.execute(sql, condition)  # 使用参数化查询来增加安全性。
#             self.connection.commit()
#             logging.info("Deleted a row of data successfully!")  # 或根据实际情况修改日志信息。
#         except Exception as e:
#             logging.error("Failed to delete data: {}".format(e))  # 或根据实际情况修改日志信息。
#             return False  # 或根据实际情况修改返回值。
#         return True  # 或根据实际情况修改返回值。
#
#     def delete_multiple_data(self,
#                              conditions):  # conditions是一个列表，包含多个删除条件。例如：[{'name': 'Name1', 'age': 20}, {'name': 'Name2', 'age': 30}]。
#         try:
#             with self.connection.cursor() as cursor:  # 使用事务来确保数据一致性。
#                 for condition in conditions:  # 根据每个条件构建SQL语句。注意：这里使用游标循环而不是SQL的IN子句，因为IN子句对每个条件单独执行DELETE操作，效率较低。如果你有大量的条件，考虑